//
//  OPIntroduceViewController.m
//  OnePerson
//
//  Created by BaoWanPei on 16/6/11.
//  Copyright © 2016年 BWP. All rights reserved.
//

#import "OPIntroduceViewController.h"
#import "YYImage.h"
#import "AppDelegate.h"

@interface OPIntroduceViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, weak) UIButton *entryButton;

/** 存放gif图片 */
@property (nonatomic, strong) NSArray *gifNames;
/** 当前展示图片的下标 */
@property (nonatomic, assign) NSInteger currentIndex;
/** 最后一张的下标 */
@property (nonatomic, assign) NSInteger lastIndex;
/** 存放展示gif图片的View */
@property (nonatomic, strong) NSArray *yyImageViews;

@property (strong, nonatomic) YYAnimatedImageView *yyImageView0;
@property (strong, nonatomic) YYAnimatedImageView *yyImageView1;
@property (strong, nonatomic) YYAnimatedImageView *yyImageView2;
@property (strong, nonatomic) YYAnimatedImageView *yyImageView3;

@end

@implementation OPIntroduceViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化数据
    [self initDatas];
    
    // 设置scrollView及里面展示iamge的ImageView
    [self setUpViews];

}

- (void)viewDidAppear:(BOOL)animated{
    // View加载完成就立即开始播放gif图片
    [super viewDidAppear:animated];
    [self playCurrentAnimation];
}

/** 初始化 所需数据 */
- (void)initDatas{
    self.gifNames = @[@"1.gif", @"2.gif", @"3.gif", @"4.gif"];
    self.currentIndex = 0;
    self.lastIndex = 0;
    
    _yyImageView0 = [YYAnimatedImageView new];
    _yyImageView1 = [YYAnimatedImageView new];
    _yyImageView2 = [YYAnimatedImageView new];
    _yyImageView3 = [YYAnimatedImageView new];
    self.yyImageViews = @[_yyImageView0, _yyImageView1, _yyImageView2, _yyImageView3];

}

/** 添加ScrollView */
- (void)setUpViews{
    
    // 创建ScrollView
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.gifNames.count, 0);
    scrollView.delegate =self;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.scrollView = scrollView;
    
    // 添加imageView
    for (NSInteger i = 0; i < self.gifNames.count; i++) {
        YYAnimatedImageView *imageView = self.yyImageViews[i];
        imageView.autoPlayAnimatedImage = NO;
        imageView.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_scrollView addSubview:imageView];
    }
   
    // 添加PageControl
    UIPageControl *pageControl = [UIPageControl new];
    pageControl.numberOfPages = self.gifNames.count;
    pageControl.pageIndicatorTintColor = [UIColor colorWithHexString:@"6D7B90" andAlpha:0.5];
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRGBHex:0x6D7B90];
    [self.view addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-150);
    }];
    self.pageControl = pageControl;

    // 添加最后一页的进入主界面的button
    UIButton *entryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [entryButton setBackgroundImage:[UIImage imageNamed:@"skip"] forState:UIControlStateNormal];
    [entryButton addTarget:self action:@selector(entryMainViewController) forControlEvents:UIControlEventTouchUpInside];
    entryButton.alpha = 0;
    [self.view addSubview:entryButton];
    [entryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(134, 46));
        make.center.equalTo(pageControl);
    }];
    self.entryButton = entryButton;
    
}

/** 点击按钮跳转到首页 */
- (void)entryMainViewController{
    // 新特性界面展示完毕之后保存 两个版本号
    NSString *version = [MLBUtilities appCurrentVersion];
    NSString *build = [MLBUtilities appCurrentBuild];
    NSString *versionAndBuild = [NSString stringWithFormat:@"%@_%@", version, build];
    [UserDefaults setObject:versionAndBuild forKey:MLBLastShowIntroduceVersionAndBuild];
    [(AppDelegate *)[UIApplication sharedApplication].delegate showMainTabBarControllers];
}

- (void)playCurrentAnimation {
    if (_lastIndex != _currentIndex) {
        YYAnimatedImageView *lastImageView = _yyImageViews[_lastIndex];
        if (lastImageView.currentIsPlayingAnimation) {
            [lastImageView stopAnimating];
            lastImageView.image = nil;// 不在视野范围内的image = nil
        }
    }
    
    YYAnimatedImageView *currentImageView = _yyImageViews[_currentIndex];
    if (!currentImageView.currentIsPlayingAnimation) {
        currentImageView.image = [YYImage imageNamed:_gifNames[_currentIndex]];
        [currentImageView startAnimating];
    }
}

// 根据索引判断分页控制器和按钮的显示与隐藏
- (void)scrollViewCurrentPageDidChangedTo:(NSInteger)pageIndex {
    _currentIndex = pageIndex;
    if (_currentIndex == self.gifNames.count - 1) {
        _pageControl.alpha = 0;
        _entryButton.alpha = 1;
    } else {
        _pageControl.alpha = 1;
        _entryButton.alpha = 0;
    }
    
    [self playCurrentAnimation];
    _lastIndex = _currentIndex;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint contentOffset = scrollView.contentOffset;
    
    NSInteger currentPage = contentOffset.x / SCREEN_WIDTH;
    BOOL hasGreatThanHalfScreen = contentOffset.x > (currentPage + 0.5) * SCREEN_WIDTH;
    _pageControl.currentPage = currentPage + (hasGreatThanHalfScreen ? 1 : 0);
    
    NSInteger leftLimit = (self.gifNames.count - 2 + 0.5) * SCREEN_WIDTH;
    NSInteger rightLimit = (self.gifNames.count - 1) * SCREEN_WIDTH;
    if (contentOffset.x >= leftLimit && contentOffset.x <= rightLimit) {
        CGFloat buttonAlpha = (contentOffset.x - leftLimit) / (SCREEN_WIDTH / 2);
        _entryButton.alpha = buttonAlpha;
        _pageControl.alpha = 1 - buttonAlpha;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewCurrentPageDidChangedTo:scrollView.contentOffset.x / SCREEN_WIDTH];
}


@end


